package org.xinjiang.tarim.travel.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.xinjiang.tarim.travel.service.TopicService;
import org.xinjiang.tarim.travel.utils.Constants;
import org.xinjiang.tarim.travel.utils.JsonResult;
import org.xinjiang.tarim.travel.vo.Criteria;
import org.xinjiang.tarim.travel.vo.PageBean;

import javax.servlet.http.HttpServletRequest;

/**
 * 话题
 */
@CrossOrigin
@RestController
@RequestMapping("/api/topic")
public class TopicApi {

    @Autowired
    TopicService topicService;

    /**
     * 条件查询（分页）
     * @param pageIndex
     * @param pageSize
     * @param topicTitle
     * @return
     */
    @RequestMapping(value = "/select")
    public JsonResult select(HttpServletRequest request,
                             @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        JsonResult jsonResult;
        try {
            String topicTitle = request.getParameter("topicTitle");
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("topicTitle", topicTitle);
            pageBean = topicService.selectByCondition(criteria, pageBean);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

}
